import {createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
      {
          path: '/',
          redirect: '/login', // login
      },
      {
          path: '/login',
          meta: {
              // title: '课程管理系统' // 用默认的
          },
          component: () => import('@/views/Login.vue'),
      },
      {
          path: '/student',
          meta: {
              title: '学生管理'
          },
          component: () => import('@/views/student/Student.vue'),
          redirect: '/student/home',
          children: [
              {
                  path: 'home',
                  meta: {
                      title: '学生首页'
                  },
                  component: () => import('@/views/student/StudentHome.vue')
              },
              {
                  path: 'course-select',
                  meta: {
                      title: '学生选修课程'
                  },
                  component: () => import('@/views/student/StudentCourseSelect.vue')
              },
              {
                  path: 'course',
                  meta: {
                      title: '学生课程信息'
                  },
                  component: () => import('@/views/student/StudentCourse.vue')
              },
              {
                  path: 'time-table',
                  meta: {
                      title: '学生课程表'
                  },
                  component: () => import('@/views/student/StudentTimeTable.vue')
              },
              {
                  path: 'exam',
                  meta: {
                      title: '成绩查询'
                  },
                  component: () => import('@/views/student/StudentExam.vue')
              },
              {
                  path: 'info',
                  meta: {
                    title: '学生个人信息'
                  },
              component: () => import('@/views/student/StudentInfo.vue')
              },
          ]
      },
      {
          path: '/teacher',
          meta: {
              title: '教师管理'
          },
          component: () => import('@/views/teacher/Teacher.vue'),
          redirect: '/teacher/home',
          children: [
              {
                  path: 'home',
                  meta: {
                      title: '教师首页'
                  },
                  component: () => import('@/views/teacher/TeacherHome.vue')
              },
              {
                  path: 'course',
                  meta: {
                      title: '教师课程信息'
                  },
                  component: () => import('@/views/teacher/TeacherCourse.vue')
              },
              {
                  path: 'time-table',
                  meta: {
                      title: '教师课表'
                  },
                  component: () => import('@/views/teacher/TeacherTimeTable.vue')
              },
              {
                  path: 'grade',
                  meta: {
                      title: '教师成绩录入'
                  },
                  component: () => import('@/views/teacher/TeacherGrade.vue')
              },
          ]
      },
      {
          path: '/admin',
          meta: {
              title: '管理员管理'
          },
          component: () => import('@/views/admin/Admin.vue'),
          redirect: '/admin/home',
          children: [
              {
                  path: 'home',
                  meta: {
                      title: '管理员首页'
                  },
                  component: () => import('@/views/admin/AdminHome.vue')
              },
              {
                path: 'dept',
                meta: {
                    title: '学院管理'
                },
                  component: () => import('@/views/admin/AdminDept.vue')
              },
              {
                  path: 'major',
                  meta: {
                      title: '专业管理'
                  },
                  component: () => import('@/views/admin/AdminMajor.vue')
              },
              {
                  path: 'class',
                  meta: {
                      title: '班级管理'
                  },
                  component: () => import('@/views/admin/AdminClass.vue')
              },
              {
                  path: 'student',
                  meta: {
                      title: '学生管理'
                  },
                  component: () => import('@/views/admin/AdminStudent.vue')
              },
              {
                  path: 'teacher',
                  meta: {
                      title: '教师管理'
                  },
                  component: () => import('@/views/admin/AdminTeacher.vue')
              },
              {
                  path: 'course',
                  meta: {
                      title: '课程管理'
                  },
                  component: () => import('@/views/admin/AdminCourse.vue')
              },
              {
                  path: 'student-course',
                  meta: {
                      title: '选课管理'
                  },
                  component: () => import('@/views/admin/AdminStudentCourse.vue')
              },
              {
                  path: 'info',
                  meta: {
                      title: '管理员管理'
                  },
                  component: () => import('@/views/admin/AdminInfo.vue')
              }
          ]
      },
      {
          path: '/:catchAll(.*)',
          meta: {
              title: '404'
          },
          component: () => import('../views/404.vue'),
      }
  ],
})

// 添加全局前置守卫来设置页面标题
router.beforeEach((to, from, next) => {
    // 设置页面标题
    if (to.meta.title) {
        document.title = to.meta.title + ' - 课程管理系统'
    } else {
        document.title = '课程管理系统' // 默认标题
    }
    next()
})

export default router